package com.geekchique.runningbuddy.view.mapofjog;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;

import com.geekchique.runningbuddy.activity.MapOfJog;
import com.google.android.maps.Overlay;
import com.google.android.maps.Point;
import com.google.googlenav.DrivingDirection;
import com.google.googlenav.map.MapPoint;
import com.geekchique.runningbuddy.R;

public class MapOverlay extends Overlay {
	
	private static final int X = 0;
	private static final int Y = 1;

	private final android.graphics.Point PIN_HOTSPOT = new android.graphics.Point(5,29);
	
	MapOfJog drivingDirectionsActivity = null;
	DrivingDirection drivingDirections = null;
	
	private Bitmap PIN_START = null;
	private Bitmap PIN_END = null;
	private Bitmap INFO_LOWER_LEFT = null;
	private PixelCalculator pixelCalculator;
	private int[] screenCoords;

	public MapOverlay(MapOfJog map, Bitmap startLocationImage, Bitmap endLocationImage, Bitmap infoImageBG) {
		drivingDirectionsActivity = map;
		PIN_START = startLocationImage;
		PIN_END = endLocationImage;
		INFO_LOWER_LEFT = infoImageBG;
	}

	public void draw(Canvas canvas, PixelCalculator pixelCalc, boolean withShadows) {
		super.draw(canvas, pixelCalc, withShadows);
		this.pixelCalculator = pixelCalc;
		setScreenCoOrdinates(new int[2]);
		
		drivingDirections = drivingDirectionsActivity.getDrivingDirections();
		
		if (drivingDirections != null) {
			MapPoint startPoint = drivingDirections.getStartPoint();
			MapPoint endPoint = drivingDirections.getEndPoint();
			Path drivingDirectionsPath = new Path();

			convertGeoCoOrdinatesToScreenCoOrdinates(startPoint);
			drivingDirectionsPath.moveTo(getXCoOrdinates(), getYCoOrdinates());
			
			if (drivingRouteIsNotTooLong()) {	
				MapPoint[] route = drivingDirections.getRoute();
				
				if(route == null){
					return;
				}
				
				drawSVGpath(getCurrentScreenCoOrdinates(), drivingDirectionsPath, route);
			}
			
			canvas.drawPath(drivingDirectionsPath, getRoutePathPainter());
			
			dropPinMarker(canvas, PIN_START, startPoint);
			dropPinMarker(canvas, PIN_END, endPoint);
			drawDetailsIndicator(canvas);
		}
	}

	private int getYCoOrdinates() {
		return screenCoords[Y];
	}

	private int getXCoOrdinates() {
		return screenCoords[X];
	}

	private int[] getCurrentScreenCoOrdinates() {
		return this.screenCoords;
	}

	private void setScreenCoOrdinates(int[] currentCoOrdinatesOnScreen) {
		this.screenCoords = currentCoOrdinatesOnScreen;
	}

	private void drawDetailsIndicator(Canvas canvas) {
		int totalHeightOfView = this.drivingDirectionsActivity.findViewById(R.id.map).getHeight();
		
		canvas.drawBitmap(
				this.INFO_LOWER_LEFT, 
				0,
				totalHeightOfView - this.INFO_LOWER_LEFT.height(),
				getRoutePathPainter());
		
		String time = drivingDirections.getFormattedTime().replace("Time: ", "");
		
		canvas.drawText(time, 5, totalHeightOfView - 5, getJourneyTimePainter());
		canvas.drawText(drivingDirections.getFormattedDistance(), 4, totalHeightOfView - 35, getJourneyDistPainter()); // 2, 271
		
	}

	private void dropPinMarker(Canvas canvas, Bitmap image, MapPoint startPoint) {
		convertGeoCoOrdinatesToScreenCoOrdinates(startPoint);
		canvas.drawBitmap(
				image, 
				getXCoOrdinates() - PIN_HOTSPOT.x, 
				getYCoOrdinates() - PIN_HOTSPOT.y, 
				getRoutePathPainter());
	}


	private void drawSVGpath(int[] screenCoords, Path path, MapPoint[] drivingRoute) {
		for (MapPoint mapPoint : drivingRoute) {
			if(mapPoint != null){
				convertGeoCoOrdinatesToScreenCoOrdinates(mapPoint);
				path.lineTo(getXCoOrdinates(), getYCoOrdinates());
			}
		}
	}
	
	protected Paint getJourneyTimePainter(){
		Paint paint = new Paint();
		paint.setAntiAlias(true);
		paint.setARGB(255,255,255,255);
		paint.setStrokeWidth(1);
		paint.setStyle(Paint.Style.FILL_AND_STROKE);
		paint.setTextSize(24);
		return paint;
	}
	
	protected Paint getJourneyDistPainter(){
		Paint paint = new Paint();
		paint.setAntiAlias(true);
		paint.setARGB(255,255,255,255);
		paint.setStrokeWidth(1);
		paint.setStyle(Paint.Style.FILL_AND_STROKE);
		paint.setTextSize(16);
		return paint;
	}
	
	protected Paint getRoutePathPainter(){
		Paint paint = new Paint();
		paint.setAntiAlias(true);
		paint.setStrokeWidth(4);
		paint.setARGB(100, 113, 105, 252);
		paint.setStyle(Paint.Style.STROKE);
		return paint;
	}

	private boolean drivingRouteIsNotTooLong() {
		return !drivingDirections.routeTooLong();
	}
	
	private void convertGeoCoOrdinatesToScreenCoOrdinates(MapPoint mapPoint){
		Point marker = new Point(mapPoint.getLatitude(), mapPoint.getLongitude());
		this.pixelCalculator.getPointXY(marker, getCurrentScreenCoOrdinates());
	}
}

